package com.sakhalin.news.ui.other

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.sakhalin.news.R
import com.sakhalin.news.ui.theme.Blue
import com.sakhalin.news.ui.theme.BlueDark

@Composable
fun ErrorView(errorText: String, onClickRetry: () -> Unit, modifier: Modifier = Modifier) {

    val isLightTheme = MaterialTheme.colors.isLight

    Column(
        modifier = modifier
            .fillMaxSize()
            .wrapContentSize(align = Alignment.Center),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            painter = painterResource(id = R.drawable.ic_server_offline),
            contentDescription = "",
            colorFilter = ColorFilter.tint(color = if (isLightTheme) Blue else Color.White)
        )
        Text(
            text = errorText,
            color = if (isLightTheme) Color.Black else Color.White,
            textAlign = TextAlign.Center,
            modifier = Modifier.padding(start = 10.dp, end = 10.dp)
        )
        OutlinedButton(onClick = onClickRetry, modifier = Modifier.padding(top = 10.dp)) {
            Text(text = "Повторить")
        }
    }
}

@Composable
fun ErrorItem(
    message: String,
    modifier: Modifier = Modifier,
    onClickRetry: () -> Unit
) {
    val isLightTheme = MaterialTheme.colors.isLight

    Column(
        modifier = modifier
            .fillMaxWidth()
            .wrapContentSize(align = Alignment.Center),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = message,
            maxLines = 2,
            textAlign = TextAlign.Center,
            color = if (isLightTheme) Color.Black else Color.White
        )
        OutlinedButton(onClick = onClickRetry) {
            Text(text = "Повторить")
        }
    }
}

@Composable
fun LoadingView(
    modifier: Modifier = Modifier
) {
    CircularProgressIndicator(
        color = BlueDark,
        modifier = modifier
            .fillMaxSize()
            .wrapContentSize(align = Alignment.Center)
    )
}

@Composable
fun LoadingItem() {
    CircularProgressIndicator(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentWidth(align = Alignment.CenterHorizontally)
            .size(20.dp, 20.dp)
    )
}